local assets =
{
    Asset("ANIM", "anim/tf_rainbow_apple.zip"),
}

local function OnHit(attacker, data)
    local target = data.target
    if target and target:IsValid() and target:HasTag("epic") then
        local truedamage = 10
        if target.components.health ~= nil and not target:HasTag("playerghost") then
            target.components.health:DoDelta(-truedamage, nil, "orange_jam_afflicted", nil, attacker, true) -- 无视防御
        end
        if target.components.health:IsDead() then
            attacker:PushEvent("killed", { victim = target })
            if target.components.combat ~= nil and target.components.combat.onkilledbyother ~= nil then
                target.components.combat.onkilledbyother(target, attacker)
            end
        end
    end
end

local function oneaten(inst, data)
    if data ~= nil and data.eater ~= nil then
        local eater = data.eater
        eater:AddDebuff("tf_buff_rainbow_apple", "tf_buff_rainbow_apple")
        if eater.components.planardamage and eater.components.tf_apple and eater.components.tf_apple.has_eaten == false then
            eater:ListenForEvent("onhitother", OnHit)
            eater.components.tf_apple.has_eaten = true
        end
    end
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("tf_rainbow_apple")
    inst.AnimState:SetBuild("tf_rainbow_apple")
    inst.AnimState:PlayAnimation("idle")

    MakeInventoryFloatable(inst)

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")
    inst:AddComponent("tradable")

    inst:AddComponent("edible")
    inst.components.edible.foodtype = FOODTYPE.GOODIES
    inst.components.edible.healthvalue = 200
    inst.components.edible.hungervalue = 200
    inst.components.edible.sanityvalue = 200

    inst:ListenForEvent("oneaten", oneaten)

    MakeHauntableLaunchAndPerish(inst)

    return inst
end

return Prefab("tf_rainbow_apple", fn, assets)
